﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Zeta.Common;
using Zeta.Game;
using Zeta.Game.Internals;
using Zeta.Game.Internals.Actors;
using Zeta.Game.Internals.SNO;

namespace KadalaSpree.XCore
{

    public class XItem : XActor
    {
        public XItem()
        {

        }

        public XItem(int actorSNO)
        {
            _actorId = actorSNO;
            Update();
        }

        internal DiaItem _diaItem;
        public virtual DiaItem DiaItem
        {
            get
            {
                if (!IsValid) Update();
                if (ACDBased)
                {
                    return (DiaItem)ACDItem.AsRActor;
                }
                return _diaItem;
            }
        }

        public VendorSlotType SlotType { get; set; }

        public override bool IsValid
        {
            get
            {
                if (ACDBased)
                {
                    return ACDItem != null && ACDItem.IsValid;
                }
                return _diaItem != null && _diaItem.IsValid;
            }
        }

        internal ACDItem ACDItem { get; set; }
        internal bool ACDBased { get; set; } 
        public int DynamicId { get; set; }

        internal override bool Update()
        {
            var item = ZetaDia.Actors.GetActorsOfType<DiaItem>(true).FirstOrDefault<DiaItem>(i => i.ActorSNO == ActorId);

            if (item != null && item.IsValid)
            {
                X.Logger.Instance.Log("Found Item {0} Id={1}", item.Name, ActorId);

                ACDBased = false;

                _diaObject = (DiaObject)item;
                _diaItem = item;
                _actorId = item.ActorSNO;
                
                SlotType = XVendor.GetVendorSlotType(item);

                return true;
            }
            else
            {
                var acdItem = ZetaDia.Actors.GetActorsOfType<ACDItem>(true).FirstOrDefault(i => i.ActorSNO == ActorId);

                if (acdItem != null && acdItem.IsValid)
                {
                    X.Logger.Instance.Log("Found ACDItem {0} Id={1}", acdItem.Name, ActorId);

                    ACDItem = acdItem;
                    DynamicId = acdItem.DynamicId;
                    ACDBased = true;

                    _diaObject = (DiaObject)acdItem.AsRActor;
                    _diaItem = (DiaItem)_diaObject;
                    _actorId = acdItem.ActorSNO;

                    SlotType = XVendor.GetVendorSlotType(acdItem.ItemBaseType, acdItem.ItemType, acdItem.IsTwoHand, acdItem.IsOneHand);

                    return true;
                }
            }

            X.Logger.Instance.Log("Failed to find item Id={0}", ActorId);

            return false;
        }

        public override XObjectType XObjectType { get { return XObjectType.XItem; } } 
    }

}
